<?php

class PostSystemHook
{

	function doit()
	{
		$this->write_http_cache();
	}
	
	function write_http_cache()
	{
		$CI =& get_instance();
		if (!isset($CI->http_cache_filename))
			return;
			
		$path = $CI->config->item('http_cache_path');

		$cache_path = ($path == '') ? APPPATH.'cache/' : $path;
		if ( ! is_dir($cache_path) OR ! is_really_writable($cache_path))
		{
			log_message('error', "Unable to write cache file: ".$cache_path);
			return;
		}
		if ( ! is_dir($cache_path.'/'.$CI->http_cache_filepath))
			mkdir($cache_path.'/'.$CI->http_cache_filepath, 0777, true);
		
		$cache_path .= $CI->http_cache_filepath.'/'.$CI->http_cache_filename;

		if ( ! $fp = @fopen($cache_path, FOPEN_WRITE_CREATE_DESTRUCTIVE))
		{
			log_message('error', "Unable to write cache file: ".$cache_path);
			return;
		}
		
		if (flock($fp, LOCK_EX))
		{
			fwrite($fp, $CI->output->get_output());
			flock($fp, LOCK_UN);
		}
		else
		{
			log_message('error', "Unable to secure a file lock for file at: ".$cache_path);
			return;
		}
		fclose($fp);
		@chmod($cache_path, FILE_WRITE_MODE);

		log_message('debug', "Cache file written: ".$cache_path);
	}
}

/* End of file post_system_hook.php */
/* Location: ./application/hooks/post_system_hook.php */